package com.ad.pet;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.structure.mvp.AbsBasePresenter;
import com.ad.pet.models.remote.CommList;
import com.ad.pet.models.remote.Money;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.remote.home.Home;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by SEELE on 2018/5/4.
 */
public class MainPresenter extends AbsBasePresenter implements MainContract.Presenter {
    private ApiService mApiService;
    private MainContract.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;


    @Inject
    public MainPresenter(ApiService apiService,
                         Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }

    @Override
    public void onCreate(MainContract.View view) {
        this.mView = view;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
            mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }

    @Override
    public void getActivityCoupon() {
        mRxDisposable.add(mApiService.getUserLandingPageReg()
                .subscribe(new AppSubscriber<Home>(mView) {
                    @Override
                    public void next(Home list) {
                        mView.getActivityCouponSuccess(list);
                    }
                }));
    }

    @Override
    public void getMoney() {
        mRxDisposable.add(
                mApiService.getCurrencyList().subscribe(new AppSubscriber<CommList<List<Money>>>(mView) {
                    @Override
                    public void next(CommList<List<Money>> continents) {
                        mView.getMoneySuccess(continents.getList());
                    }
                })
        );
    }

    @Override
    public void checkToken() {
        mRxDisposable.add(
                mApiService.checkToken().subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User continents) {
                       mView.checkTokenStatus(continents.getIs_valid());
                    }
                })
        );
    }
}
